import { ajax } from '../lib/ajax.js';
import { tools } from '../lib/tools.js';

let tbData = document.getElementById('tbData');
let txt_title = document.getElementById('txt_title');
let txt_info = document.getElementById('txt_info');
let sel_order = document.getElementById('sel_order');
let btnQuery = document.getElementById('btnQuery');
let btnReset = document.getElementById('btnReset');

let list = [];

let page = {
  pageNumber: 1,
  pageSize: 10,
};

let queryInfo = { order: 1, title: '', info: '' };

// 测试合并json的工具
console.log('合并json', tools.mergeJson(page, queryInfo));

btnQuery.addEventListener('click', () => {
  page.pageNumber = 1;
  query();
});

btnReset.addEventListener('click', () => {
  txt_title.value = '';
  txt_info.value = '';
  sel_order.value = 1;
  page.pageNumber = 1;
  query();
});

function query() {
  queryInfo.title = txt_title.value;
  queryInfo.info = txt_info.value;
  queryInfo.order = sel_order.value;

  ajax.get('/crud/news', tools.mergeJson(page, queryInfo), (data) => {
    if (!data.success) {
      alert(data.message);
      return;
    }
    // 数据处理
    list = data.data.data;
    page = data.data.pageBean;
    console.log('分页信息：', page);
    showData();
    showPage();
  });
}

function showData() {
  tbData.innerHTML = '';
  list.forEach((item) => {
    let tr = document.createElement('tr');
    let td;
    // 编号
    td = document.createElement('td');
    td.append(item.nid);
    tr.append(td);

    // 标题
    td = document.createElement('td');
    td.append(item.title);
    tr.append(td);

    // 来源
    td = document.createElement('td');
    td.append(item.source);
    tr.append(td);

    // 点击量
    td = document.createElement('td');
    td.append(item.hits);
    tr.append(td);

    // 发布时间
    td = document.createElement('td');
    td.append(tools.formatDate(item.lastupdate));
    tr.append(td);

    // 操作功能
    td = document.createElement('td');
    // 查看内容按钮
    let btnInfo = document.createElement('button');
    btnInfo.append('查看内容');
    btnInfo.addEventListener('click', () => {
      location.href = 'news_info.html?nid=' + item.nid;
    });
    td.append(btnInfo);

    // 删除功能
    let btnDel = document.createElement('button');
    btnDel.append('删除');
    btnDel.addEventListener('click', () => {
      del(item);
    });
    td.append(btnDel);

    // 查看内容按钮
    let btnModi = document.createElement('button');
    btnModi.append('修改');
    btnModi.addEventListener('click', () => {
      location.href = 'news_modi.html?nid=' + item.nid;
    });
    td.append(btnModi);

    tr.append(td);

    tbData.append(tr);
  });
}

let spPre = document.getElementById('spPre');
let spPage = document.getElementById('spPage');
let spNext = document.getElementById('spNext');
function showPage() {
  spPage.innerHTML = `
  当前页/总页数/记录数：
  ${page.pageNumber}/${page.pageCount}/${page.total}
  `;
}

spNext.addEventListener('click', () => {
  page.pageNumber++;
  // 不可以超过总的页数
  if (page.pageNumber > page.pageCount) {
    page.pageNumber = page.pageCount;
    return;
  }
  query();
});

spPre.addEventListener('click', () => {
  page.pageNumber--;
  // 不可以小于1
  if (page.pageNumber < 1) {
    page.pageNumber = 1;
    return;
  }
  query();
});

// 删除的功能
function del(info) {
  console.log('要删除的数据：', info);
  if (!confirm(`是否删除：${info.title}？`)) {
    return;
  }
  ajax.delete(`/crud/news/${info.nid}`, {}, (data) => {
    alert(data.message);
    query();
  });
}

query();
